<?php
Yii::import('application.models.catalog.Product');

class ProductsController extends Controller
{
    public function actionIndex()
    {
        $model=new Product();
        $criteria=new CDbCriteria();

        $sort=new CSort();
        $sort->sortVar='sort';
        $sort->defaultOrder='product_id ASC';
        $sort->multiSort=true;
        $sort->attributes = array(
            'id'=>array(
                'label'=>'id',
                'asc'=>'product_id ASC',
                'desc'=>'product_id DESC',
                'default'=>'desc',
            ),
            'model'=>array(
                'label'=>'цене',
                'asc'=>'model ASC',
                'desc'=>'model DESC',
                'default'=>'desc',
            ),
            'quantity'=>array(
                'label'=>'количеству',
                'asc'=>'quantity ASC',
                'desc'=>'quantity DESC',
                'default'=>'desc',
            ),
            'price'=>array(
                'label'=>'цене',
                'asc'=>'price ASC',
                'desc'=>'price DESC',
                'default'=>'desc',
            ),
        );

        $dataProvider=new CActiveDataProvider($model,array(
                'criteria'=>$criteria,
                'sort'=>$sort,
                'pagination'=>array(
                    'pageSize'=>Yii::app()->params['elementsPerPage'],
                ),
            )
        );
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));
    }

    public function actionView()
    {
        if(!$id=Yii::app()->request->getQuery('prodId')) $this->redirect($this->createUrl('catalog/products'));
        if(!$product=Product::model()->findByPk($id)){
            Yii::app()->notification->error=Yii::t('error','errorNoProductId',array('{id}'=>$id));
            $this->redirect(Yii::app()->homeUrl);
        }

        if(Yii::app()->request->isAjaxRequest){
            if(!$product) Yii::app()->end('Main.closePopup();');
            $count=Yii::app()->request->getQuery('count') ? Yii::app()->request->getQuery('count') : 1;
            Yii::app()->end($this->renderPartial('view',array('product'=>$product),true,false));
        }

        $this->render('view',array(
            'product'=>$product,
        ));
    }

    public function actionAddToCart()
    {
        $json=array(
            'message',
            'error',
            'id',
            'hesProducts',
            'htmlContent',
        );

        if(Yii::app()->user->isGuest){
            $json['message']=Yii::t('base', 'errorNotLogged');
            Yii::app()->end(json_encode($json));
        }

        if(!$prodId=Yii::app()->request->getQuery('prodId')){
            $json['message']=Yii::t('error','errorNoProductId',array('{id}'=>$prodId));
            Yii::app()->end(json_encode($json));
        }

        $count=Yii::app()->request->getQuery('count');
        if(!$count) $count=1;

        $json['hesProducts']=Yii::app()->cart->hesProducts;
        if(Yii::app()->cart->add($prodId,$count)){
            $message=Yii::t('shopping/cart','productAddedToCart',array('{name}'=>Yii::app()->cart->lastAdded->name));

            $json['message']=$message;
            $json['id']=$prodId;
            $json['htmlContent']=$this->renderPartial('application.views.shopping.__cart',array(),true,false);
        }else{
            $json['message']=Yii::t('shopping/cart','errorAdd').': '.Yii::app()->cart->message;
            Yii::app()->end(json_encode($json));
        }

        Yii::app()->end(json_encode($json));
    }

    public function actionRemoveFromCart()
    {
        $json=array(
            'message',
            'error',
            'id',
            'hesProducts',
        );

        $id=Yii::app()->request->getQuery('prodId');
        $remove=Yii::app()->request->getQuery('remove');

        if(!$id || !$remove){
            Yii::app()->end(json_encode($json));
        }

        if($product=Yii::app()->cart->remove($id)){
            $message=Yii::t('shopping/cart','productRemoved',array('{product}'=>$product->name));
            $json['message']=$message;
            $json['id']=$id;
            $json['hesProducts']=Yii::app()->cart->hesProducts;
        }else{
            $json['error']=Yii::t('shopping/cart','errorProductRemoved').': '.Yii::app()->cart->message;
        }

        Yii::app()->end(json_encode($json));
    }
}